package com.ruoyi.userManage.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.md5.MD5Utils;
import com.ruoyi.userManage.domain.BalanceConsumeDetail;
import com.ruoyi.userManage.domain.TicketConsumeDetail;
import com.ruoyi.userManage.domain.User;
import com.ruoyi.userManage.mapper.BalanceConsumeDetailMapper;
import com.ruoyi.userManage.mapper.TicketConsumeDetailMapper;
import com.ruoyi.userManage.mapper.UserMapper;
import com.ruoyi.userManage.service.IUserService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * 用户信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-08-12
 */
@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Resource
    private BalanceConsumeDetailMapper balanceConsumeDetailMapper;
    @Resource
    private TicketConsumeDetailMapper ticketConsumeDetailMapper;

    /**
     * 查询用户信息
     *
     * @param id 用户信息主键
     * @return 用户信息
     */
    @Override
    public User selectUserById(Long id) {
        return userMapper.selectUserById(id);
    }

    /**
     * 查询用户信息列表
     *
     * @param user 用户信息
     * @return 用户信息
     */
    @Override
    public List<User> selectUserList(User user) {
        return userMapper.selectUserList(user);
    }

    /**
     * 新增用户信息
     *
     * @param user 用户信息
     * @return 结果
     */
    @Override
    public int insertUser(User user) {
        user.setCreateTime(DateUtils.getNowDate());
        return userMapper.insertUser(user);
    }

    /**
     * 修改用户信息
     *
     * @param user 用户信息
     * @return 结果
     */
    @Override
    public int updateUser(User user) {
        User user1 = userMapper.selectUserById(user.getId());
        if (!StrUtil.isEmpty(user.getPayPass())) {
            user.setPayPass(MD5Utils.MD5(user.getPayPass()));
        }
        if (user.getIsDelete() == 1) {
            StpUtil.logout(user.getId());
            StpUtil.kickout(user.getId());
        }

        // 获取当前登录的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();

        if (user.getBalance().compareTo(user1.getBalance()) != 0) {
            // 获取整数部分
            BigDecimal integerPart = user.getBalance().setScale(0, RoundingMode.DOWN);

            // 获取小数部分
            BigDecimal fractionalPart = user.getBalance().remainder(BigDecimal.ONE);

            if (integerPart.compareTo(new BigDecimal(9999999)) >= 0 || fractionalPart.compareTo(new BigDecimal(99))>=0) {
                throw new RuntimeException("余额金额超出限制");
            }
            BalanceConsumeDetail balanceConsumeDetail = new BalanceConsumeDetail();
            balanceConsumeDetail.setUserId(user.getId());
            balanceConsumeDetail.setOperName(loginUser.getUsername());
            if (user.getBalance().compareTo(user1.getBalance()) > 0) {
                BigDecimal subtract = user.getBalance().subtract(user1.getBalance());
                balanceConsumeDetail.setPrice(subtract);
                balanceConsumeDetail.setType("ADD");
                balanceConsumeDetail.setDescribes("后台赠送" + subtract);
            } else {
                BigDecimal subtract = user1.getBalance().subtract(user.getBalance());
                balanceConsumeDetail.setPrice(subtract);
                balanceConsumeDetail.setType("REDUCE");
                balanceConsumeDetail.setDescribes("后台扣除" + subtract);
            }
            balanceConsumeDetailMapper.insertBalanceConsumeDetail(balanceConsumeDetail);
        }


        if (user.getTicket().compareTo(user1.getTicket()) != 0) {
            // 获取整数部分
            BigDecimal integerPart = user.getTicket().setScale(0, RoundingMode.DOWN);

            // 获取小数部分
            BigDecimal fractionalPart = user.getTicket().remainder(BigDecimal.ONE);

            if (integerPart.compareTo(new BigDecimal(9999999)) >= 0 || fractionalPart.compareTo(new BigDecimal(99))>=0) {
                throw new RuntimeException("漫券金额超出限制");
            }
            TicketConsumeDetail ticketConsumeDetail = new TicketConsumeDetail();
            ticketConsumeDetail.setUserId(user.getId());
            ticketConsumeDetail.setOperName(loginUser.getUsername());
            if (user.getTicket().compareTo(user1.getTicket()) > 0) {
                BigDecimal subtract = user.getTicket().subtract(user1.getTicket());

                ticketConsumeDetail.setPrice(subtract);
                ticketConsumeDetail.setType("ADD");
                ticketConsumeDetail.setDescribes("后台赠送" + subtract);
            } else {
                BigDecimal subtract = user1.getTicket().subtract(user.getTicket());
                ticketConsumeDetail.setPrice(subtract);
                ticketConsumeDetail.setType("REDUCE");
                ticketConsumeDetail.setDescribes("后台扣除" + subtract);
            }
            ticketConsumeDetailMapper.insertTicketConsumeDetail(ticketConsumeDetail);
        }
        user.setUpdateTime(DateUtils.getNowDate());
        return userMapper.updateUser(user);
    }

    /**
     * 批量删除用户信息
     *
     * @param ids 需要删除的用户信息主键
     * @return 结果
     */
    @Override
    public int deleteUserByIds(Long[] ids) {
        return userMapper.deleteUserByIds(ids);
    }

    /**
     * 删除用户信息信息
     *
     * @param id 用户信息主键
     * @return 结果
     */
    @Override
    public int deleteUserById(Long id) {
        return userMapper.deleteUserById(id);
    }
}
